#include "utils.h"
#include "init.h"
#include "student.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <QString>
#include <Windows.h>
#include <time.h>
#include <iomanip>
#include <string>
#include <sstream>
#include <QCryptographicHash>

using namespace std;
using namespace student;

namespace utils {

//添加日志
void addLog(string log) {
    ofstream outfile(logPath, ios::app);

    //生成当前时间
    time_t timep;
    time(&timep);

    //格式化当前时间
    QString fomatStr;
    tm *timem = localtime(&timep);
    QString year = QString::number(1900 + timem->tm_year);
    QString month = QString::number(1 + timem->tm_mon);
    if((1 + timem->tm_mon) < 10)
        month = "0" + month;
    QString day = QString::number(timem->tm_mday);
    if(timem->tm_mday < 10)
        day = "0" + day;
    QString hour = QString::number(timem->tm_hour);
    if(timem->tm_hour < 10)
        hour = "0" + hour;
    QString min = QString::number(timem->tm_min);
    if(timem->tm_min < 10)
        min = "0" + min;
    QString sec = QString::number(timem->tm_sec);
    if(timem->tm_sec < 10)
        sec = "0" + sec;
    fomatStr = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;

    //整体日志输出
    cout << log << endl;    //终端输出
    outfile << fomatStr.toStdString()
            << "  " << log << endl;
}

FILE* openfile(const char* DatPath, int size) { //参数为文件路径、一个结构体大小
    FILE *file = fopen(DatPath, "rb+");//打开文件
    auto i = MaxBuffer;
    if (file == NULL) {//文件打不开
        return NULL;
    } else //允许程序控制缓冲和stream 的缓冲区大小
        setvbuf(file, NULL, _IOFBF, (size * i));
    return file;
}

QString str2Qstr(string str) {
    return QString(QString::fromLocal8Bit(str.c_str()));
}

string Qstr2str(QString str) {
    return string((const char *)str.toLocal8Bit());
}

//判断string是否为num位数字
int judge_user(string str) {
    int len = str.length();
    if(len == STUIDNUM) {
        int i = 0;
        for(; i < len;) {
            if(isdigit(str[i]))
                i++;
            else break;
        }
        if (i != STUIDNUM)
            return -1;
    } else if(len == TEAIDNUM) {
        int i = 0;
        for(; i < len;) {
            if(isdigit(str[i]))
                i++;
            else break;
        }
        if (i != TEAIDNUM)
            return -1;
    } else
        return -1;
    return 1;
}
//判断string是否为num位数字
int judge_menu(string str) {
    int len = str.length();
    if(len == ISBN) {
        int i = 0;
        for(; i < len;) {
            if(isdigit(str[i]))
                i++;
            else break;
        }
        if (i != ISBN)
            return -1;
    }

    else
        return -1;
    return 1;
}
string UtfToGbk(const char* utf8) {
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    wchar_t* wstr = new wchar_t[len + 1];
    memset(wstr, 0, len + 1);
    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    char* str = new char[len + 1];
    memset(str, 0, len + 1);
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    if(wstr) delete[] wstr;
    return str;
}
int isDigitStr(QString src) {
    QByteArray ba = src.toLatin1();//QString 转换为 char*
    const char *s = ba.data();

    while(*s && *s >= '0' && *s <= '9') s++;

    if (*s) {
        //不是纯数字
        return 0;
    } else {
        //纯数字
        return 1;
    }
}

QString fomatTime(time_t timep) {
    QString fomatStr;
    tm *timem = localtime(&timep);
    QString year = QString::number(1900 + timem->tm_year);
    QString month = QString::number(1 + timem->tm_mon);
    QString day = QString::number(timem->tm_mday);
    fomatStr = year + "-" + month + "-" + day;
    return fomatStr;
}
//全为空格为0，否在为1
int judgeSpace(string str) {
    for(int i = 0; i < str.size(); i++) {
        if(str[i] == ' ') {
            continue;
        } else
            return 1;
    }
    return 0;
}

//hash
string sha1(string str) {
    QString qstr = QString::fromStdString(str);
    QByteArray hashData = QCryptographicHash::hash(qstr.toLocal8Bit(), QCryptographicHash::Sha1);
    return (hashData.toHex()).toStdString();
}

string rand_str(const int len) { /*参数为字符串的长度*/
    /*初始化*/
    string str; /*声明用来保存随机字符串的str*/
    char c;     /*声明字符c，用来保存随机生成的字符*/
    int idx;    /*用来循环的变量*/
    srand((unsigned)clock());
    /*循环向字符串中添加随机生成的字符*/
    for (idx = 0; idx < len; idx++) {
        /*rand()%26是取余，余数为0~25加上'a',就是字母a~z,详见asc码表*/
        c = 'a' + rand() % 26;
        str.push_back(c); /*push_back()是string类尾插函数。这里插入随机字符c*/
    }
    return str; /*返回生成的随机字符串*/
}

}
